<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta charset="utf-8" />
	<title></title>
	<link href="../css/bootstrap.min.css" rel="stylesheet">
	<link href="../css/bootstrap-select.css" rel="stylesheet">
	<link rel="stylesheet" href="../css/bootstrap-datetimepicker.min.css">
	<link href="../css/font-awesome.min.css" rel="stylesheet">
	<link href="../css/JyyCommon.css" rel="stylesheet">
	<link href="../css/JyyTable.css" rel="stylesheet">
	<link href="../css/JyyTree.css" rel="stylesheet">
	<link href="../css/JyyMsgBox.css" rel="stylesheet">
	<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
	<script type="text/javascript" src="../js/jquery.form.min.js"></script>
	<script type="text/javascript" src="../js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-datetimepicker.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-select.js"></script>
	<script type="text/javascript" src="../js/jyy_modal.js"></script>
	<script type="text/javascript" src="../js/JyyCommon.js"></script>
	<script type="text/javascript" src="../js/JyyTable.js"></script>
	<script type="text/javascript" src="../js/JyyTree.js"></script>
	<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
	<script type="text/javascript" src="../js/JyyLoading.js"></script>
    <script type="text/javascript" src="../../utils/CommonUtil.js"></script>
	<style>
		.heidenbt {
			display: none
		}

		.tableA {
			cursor: pointer;
		}
	</style>
	<script type="text/javascript">
		var tree = null;
		var table = null;
		var scheme = null;
		var treeCode = null;
		var isDetail = 0;
		var groupId = "";
		var unitname = "";
        var userId = '';
        let baseURL = localStorage.getItem("baseURL");
        $(function () {
            //加载树组件
            var treeData = getTreeData();
            tree = RxTree.init("tree", treeData, { listMode: "tree", draggable: true, checkType: "single", drillType: "1" }, { width: "200px", bottom: "0px", top: "0px", border: "none", borderRight: "1px solid #e0e0e0" }, onTreeEvent);
            tree.expandAll();
            tree.checkNode("801", true);
            getUserId();
        })

        function getUserId() {
            $.ajax({
                type: "POST",
                url: "page/pageInfo/getUserId",
                success: function (data) {
                    userId = data.respData;
                }
            });
        }
		function onTreeEvent(event) {
			console.log("onTreeEvent");
			if (event.name == "onclick") {
				var data = event.node.data;
				if (data.isLeaf) {
					$("#table").css("display", "block");
					$("#operate").css("display", "block");
					$("#term").css("display", "block");
					$("#searchName").css("display", "block");
					$("#term").val("");
					$("#selectSearch").val("");
					$("#selectSearch").css("display", "none");
					$("#iframe").css("display", "none");
					$("#operate").css("height", "50px");
					$(".col-md-12").css("top", "10px");
					$("#alltitle").css("display", "none");
					$("#table").css("top", "50px");
					$("#reset").removeClass("heidenbt")
					$("#select").removeClass("heidenbt")
					treeCode = data.code;
					groupId = "";
					unitname = "";
					setlabel(data.code);
					JyyLoading.start();
					$.when(getTableData(data.code)).done(function (tableData) {
						if (table == null) {
							table = JyyTable.init("table", tableData, { position: "absolute", top: "50px", left: "205px", right: "5px", bottom: "5px", overflow: "hidden" }, onTableEvent);
						} else {
							table.refresh(tableData);
						}
						JyyLoading.end();
					});
				}
			}
		}
		function setlabel(code) {
			$("#daochu").addClass("heidenbt")
			$("#daoru").addClass("heidenbt")
			$("#xinzeng").addClass("heidenbt")
			$("#shanchu").addClass("heidenbt")
			$("#xianshi").addClass("heidenbt")
			var label = "";
			if (code == "801") {
				$("#daochu").removeClass("heidenbt")
				$("#daoru").removeClass("heidenbt")
				$("#shanchu").removeClass("heidenbt")
				label = "节点名称";
			} else if (code == "802") {
				$("#shanchu").removeClass("heidenbt")
				$("#daochu").removeClass("heidenbt")
				$("#daoru").removeClass("heidenbt")
				label = "电厂名称";
			} else if (code == "811" || code == "812" || code == "831" || treeCode == "841" || treeCode == "842" || treeCode == "2852") {
				$("#xinzeng").removeClass("heidenbt")
				$("#shanchu").removeClass("heidenbt")
				label = "分组名称";
			} else if (code == "851") {
				$("#xinzeng").removeClass("heidenbt")
				$("#shanchu").removeClass("heidenbt")
				$("#daochu").removeClass("heidenbt")
				$("#daoru").removeClass("heidenbt")
				label = "电源名称";
			}
			$("#searchName").text(label);
		}

		function onTableEvent(type, value) {
			console.log("onTableEvent");
			var code = tree.getSelectData().code;
			var checkArr = [];
			if (treeCode == "811" && groupId != "") {
				checkArr = [2, 3, 4];
			} else if (treeCode == "812" && groupId != "") {
				checkArr = [2, 3];
			}
			if ((treeCode == "812" || treeCode == "811" || treeCode == "831" || treeCode == "841" || treeCode == "842" || treeCode == "2852") && groupId == "") {
				checkArr = [2];
			}
			if (treeCode == "851") {
				checkArr = [1, 2, 4, 11, 12, 49, 50];
			}
			if (treeCode == "2852" && groupId != "") {
				checkArr = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51];
			}
			if (treeCode == "2852" && groupId == "") {
				checkArr = [2];
			}
			if (type == "insertBefore") {
				console.log("insertBefore");
				//核查数据正确性
				for (var a = 0; a < value[1].length; a++) {
					if (table.config.hideCols.indexOf(a) <= -1) {
						if (checkArr.length == 0 || checkArr.indexOf(a) != -1) {
							if (JyyCommon.isEmpty(value[1][a])) {
								JyyMsgBox.alert({ title: "警告", message: "[" + table.config.data[0][a - 1].name + "]不可为空", type: "warning" }).on(function (e) { });
								return false;
							} else if ((!JyyCommon.isObject(value[1][a]) && value[1][a].indexOf("请选择") > -1) || value[1][a] == " ") {
								JyyMsgBox.alert({ title: "警告", message: "[" + table.config.data[0][a - 1].name + "]下拉框未做选择", type: "warning" }).on(function (e) { });
								return false;
							} else if (JyyCommon.isObject(value[1][a]) && JyyCommon.isEmpty(value[1][a].name)) {
								JyyMsgBox.alert({ title: "警告", message: "[" + table.config.data[0][a - 1].name + "]不可为空", type: "warning" }).on(function (e) { });
								return false;
							} else if (treeCode == "851" && table.config.data[0][a - 1].name == "清洁电量占比" && (value[1][a] > 1 || value[1][a] < 0)) {
								JyyMsgBox.alert({ title: "警告", message: "[" + table.config.data[0][a - 1].name + "]不能超过0-1的范围", type: "warning" }).on(function (e) { });
								return false;
							}
						}
					}
				}
				return true;
			} else if (type == "data") {
				JyyLoading.start();
				var url = "page/check/" + code;
				var keyword = $("#term").val();
				if (groupId != null && groupId != '') {
					url = "page/check/queryDeatils/" + code;
					keyword = $("#selectSearch").val();
				}
				$.ajax({
					type: "POST",
					url: url,
					data: { "value": JSON.stringify(value), "groupId": groupId, "projectid": parent.planId },
					     // contentType:"application/json",
					success: function (data) {
						var pageSize = table.config.page.option.pageSize;
						$.when(getTableData(code, 1, pageSize, keyword, groupId)).done(function (tableData) {
							table.refresh(tableData);
							JyyLoading.end();
						});
					}
				});
			} else if (type == "page") {
				console.log("page");
				var val = $("#term").val();
				var code = tree.getSelectData().code;
				JyyLoading.start();
				$.when(getTableData(code, value.currentPage, value.pageSize, val, groupId)).done(function (tableData) {
					table.refresh(tableData);
					JyyLoading.end();
				});
			}
		}
		//新增按钮
		function addRow() {
			table.addRow();
		}
		//修改按钮
		function updateRow() {
			if ((treeCode == "811" || treeCode == "812" || treeCode == "831" || treeCode == "841" || treeCode == "842" || treeCode == "2852") && groupId == "") {
				var checked = $("#table").find('table tbody tr input[type=checkbox]:checked');
				checked.each(function () {
					var tr = $(this).parents('tr')[0];
					var val = tr.children[2].innerText;
					if (val == "") return true;
					$(tr.children[2]).html(val);
				});
			}
			table.updateRows();
		}
		//取消修改按钮
		function cancelRow() {
			if ((treeCode == "811" || treeCode == "812" || treeCode == "831" || treeCode == "841" || treeCode == "842" || treeCode == "2852") && groupId == "") {
				var checked = $("#table").find('table tbody tr input[type=checkbox]:checked');
				checked.each(function () {
					var tr = $(this).parents('tr')[0];
					var id = tr.children[1].innerText;
					var val = tr.children[2].textContent;
					$(tr.children[2]).html("<a class='tableA' onclick='tabOnclick(this);' groupid='" + id + "'>" + val + "</a>");
				});
			}
			table.cancelRow();
		}
		//删除按钮
		function deleteRow() {
			table.deleteRows();
		}
		//保存按钮
		function saveRow() {
			table.saveRow();
		}
        function getFileName(){
            if(treeCode == 801) return "节点经纬度";
            if(treeCode == 802) return "电厂经纬度";
            if(treeCode == 811) return "机组档案";
            if(treeCode == 812) return "装机类型";
            if(treeCode == 831) return "典型负荷曲线";
            if(treeCode == 841) return "新能源风区";
            if(treeCode == 842) return "新能源光区";
            if(treeCode == 851) return "区外电源管理";
            if(treeCode == 852) return "指定出力";
            if(treeCode == 853) return "三段式出力";
            if(treeCode == 854) return "指定电量";
            if(treeCode == 1852) return "指定出力";
            if(treeCode == 1853) return "三段式出力";
            if(treeCode == 2852) return "其他电源管理";
            return "未知文件";
        }
		//下载按钮
		function download() {
            var form = $('<form id="download" action="'+baseURL+'pageExcel/downloadExcel/'+ treeCode +'" style="display:none" method="post">');
            if(groupId != null && groupId != "") {
                var input1 = $('<input type="hidden" name="groupId">');
                input1.attr("value",groupId);
                form.append(input1);
                var input2 = $('<input type="hidden" name="unittype">');
                input2.attr("value",$("#selectSearch").val());
                form.append(input2);
            }
            $("body").append(form);
            formSubmit(document.querySelector('#download'), getFileName());
            form.submit();
            form.remove();

		}
		//上传按钮
		function upload() {
			var file = document.getElementById("file");
			if (file.value.indexOf('xls') == -1) {
				JyyMsgBox.alert({ title: "警告", message: "请选择正确的Excel文件", type: "warning" }).on(function (e) { });
				return;
			}
			JyyLoading.start();
			var fileObj = file.files[0];
			var url = baseURL+"pageExcel/loadExcel/" + treeCode;

			var form = new FormData();
			form.append("file", fileObj);
			form.append("schemeid", parent.planId);
			if (groupId != null && groupId != "") {
				form.append("groupId", groupId);
			}
			var xhr = new XMLHttpRequest();
			xhr.open("post", url, true);
            xhr.setRequestHeader('token', JSON.parse(localStorage.getItem('vuex')).token);
			xhr.onreadystatechange = function () {
				if (xhr.readyState === 4 && xhr.status === 200) {
					var pageSize = table.config.page.option.pageSize;
					$.when(getTableData(treeCode, 1, pageSize, null, groupId)).done(function (tableData) {
						$("#term").val("");
						$("#selectSearch").val("");
						document.getElementById("file").value = null
						table.refresh(tableData);
						JyyLoading.end();
					});
				}
				if (xhr.readyState === 4 && xhr.status === 500) {
					JyyLoading.end();
				}
			}
            xhr.send(form);
		}
		//全量上传事件
		function allDownloads(event) {
			if (event.value == "") {
				return;
			}
			JyyLoading.start();
			$("#form1").find("input[name='schemeid']").val(parent.planId);
			$("#form1").ajaxSubmit({
				success: function (data) {
					alert(data);
					location.reload();
				}
			});
		}
		//显示隐藏列按钮
		function colsState(e) {
			var i = $(e).find('i');
			if ($(i).is('.glyphicon-align-justify')) {
				$(i).removeClass('glyphicon-align-justify')
				$(i).addClass('glyphicon-list');
				$(e).html($(e).html().replace("显示隐藏列", "隐藏默认列"));
				table.hideCols(false);
			} else {
				$(i).removeClass('glyphicon-list')
				$(i).addClass('glyphicon-align-justify');
				$(e).html($(e).html().replace("隐藏默认列", "显示隐藏列"));
				table.hideCols(true);
			}
		}
		//查询
		function select() {
			var value = $("#term").val();
			if ((treeCode == "811" || treeCode == "812" || treeCode == "2852") && groupId != "") {
				value = $("#selectSearch").val();
			}

			var code = tree.getSelectData().code;
			//var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;

			JyyLoading.start();
			$.when(getTableData(code, currentPage, pageSize, value, groupId)).done(function (tableData) {
				table.refresh(tableData);
				JyyLoading.end();
			});
		}
		//重置
		function reset() {
			$("#term").val("");
			$("#selectSearch").val("");

			//	var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;

			JyyLoading.start();
			$.when(getTableData(treeCode, currentPage, pageSize, null, groupId)).done(function (tableData) {
				table.refresh(tableData);
				JyyLoading.end();
			});
		}

		function getTreeData() {
			return [
				{
					code: "800", name: "坐标管理", draggable: false, level: 1, items: [
						{ code: "801", name: "节点", isLeaf: true, draggable: false, items: [] },
						{ code: "802", name: "电厂", isLeaf: true, draggable: false, items: [] }
					]
				},
				{
					code: "810", name: "机组属性管理", draggable: false, level: 1, items: [
						{ code: "811", name: "机组档案", isLeaf: true, draggable: false, items: [] },
						{ code: "812", name: "装机类型", isLeaf: true, draggable: false, items: [] }
					]
				},
				{
					code: "830", name: "负荷管理", draggable: false, level: 1, items: [
						{ code: "831", name: "典型负荷曲线", isLeaf: true, draggable: false, items: [] }
					]
				},
				{
					code: "840", name: "新能源分区管理", draggable: false, level: 1, items: [
						{ code: "841", name: "风区", isLeaf: true, draggable: false, items: [] },
						{ code: "842", name: "光区", isLeaf: true, draggable: false, items: [] }
					]
				},
				{
					code: "850", name: "区外管理", draggable: false, level: 1, items: [
						{ code: "851", name: "区外电源管理", isLeaf: true, draggable: false, items: [] },
						{ code: "2852", name: "其他电源管理", isLeaf: true, draggable: false, items: [] }
					]
				}
			];
		}
		function getTableData(code, currentPage, pageSize, keyword, groupId) {
			var defer = $.Deferred();
			var result = null;
			var url = "page/pageInfo/" + code;
			if (groupId != null && groupId != '') {
				url = "page/pageInfo/queryDeatils/" + code;
			}

			$.ajax({
				dataType: "json",
				async: true,
				data: { "pagenow": currentPage, "pagenum": pageSize, "groupId": groupId, "keyword": keyword, "projectid": parent.planId },
				url: url
			}).done(function (res) {
                if(res.respCode == 200){
                    var sts = eval("("+res.respData+")");
                    result = sts;
                    defer.resolve(sts);
                } else {
                    JyyMsgBox.alert({ title: "错误", message: res.respMessage, type: "error" }).on(function (e) { });
                    return;
                }
			});
			return defer.promise();
		}
		function tabOnclick(e) {
			isDetail = 1;
			groupId = e.getAttribute("groupid");
			$("#titleName").text(e.text);
			$("#operate").css("height", "70px");
			$(".col-md-12").css("top", "30px");
			$("#alltitle").css("display", "block");
			$("#term").css("display", "none");
			if (treeCode == "811" || treeCode == "2852") {
				$("#managerTitle").text("机组管理>");
			} else if (treeCode == "812") {
				$("#managerTitle").text("机组类型>");
			} else if (treeCode == "831") {
				$("#managerTitle").text("典型负荷曲线>");
				$("#selectSearch").css("display", "none");
				$("#searchName").css("display", "none");
			} else if (treeCode == "841") {
				$("#managerTitle").text("风区>");
				$("#selectSearch").css("display", "none");
				$("#searchName").css("display", "none");
			} else if (treeCode == "842") {
				$("#managerTitle").text("光区>");
				$("#selectSearch").css("display", "none");
				$("#searchName").css("display", "none");
			}
			if (treeCode != "831" && treeCode != "841" && treeCode != "842") {
				$("#selectSearch").css("display", "block");
				$("#searchName").text("机组类型");
				$.ajax({
					url: "page/pageInfo/getUnitType",
					dataType: "json",
					async: true,
					success: function (res) {
                        var data = res.respData;
						var select = $("#selectSearch");
						select.append("<option value=''>全部</option>");
						for (var i = 0; i < data.length; i++) {
							select.append("<option value='" + data[i].CODE + "'>"
								+ data[i].NAME + "</option>");
						}
						$('.selectpicker').selectpicker('val', '');
						$('.selectpicker').selectpicker('refresh');
					}
				});
			} else {
				$("#reset").addClass("heidenbt")
				$("#select").addClass("heidenbt")
			}
			$("#daochu").removeClass("heidenbt")
			$("#daoru").removeClass("heidenbt")
			var pageSize = table.config.page.option.pageSize;
			$.when(getTableData(treeCode, 1, pageSize, null, groupId)).done(function (tableData) {
				table = JyyTable.init("table", tableData, { position: "absolute", top: "70px", left: "205px", right: "5px", bottom: "5px", overflow: "hidden" }, onTableEvent);
				JyyLoading.end();
			});
		}

		function pageOnClick(e) {
			unitname = e.getAttribute("unitname");
			$("#tabTitle").text(e.text);
			$('#tabsEditData').draggable();
			$('#tabsEditData').css('display', 'block');
			$('#contentPanel').attr('src', './mapDataPopup.html?unitname=' + encodeURI(unitname) + '&type=' + encodeURI(e.text));
		}
		function closeTabsEditData() {
			$('#tabsEditData').css('display', 'none');
		}
	</script>
</head>

<body style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;overflow-x: hidden;">
	<div id="operate" style="position: absolute;top: 0px;left: 200px;right: 0px;height: 50px;">
		<div id="alltitle" style="width: 100%;display: none;margin: 5px;"><span id="managerTitle"></span><span
				id="titleName"></span></div>
		<div class="col-md-12" style="position: absolute;top: 10px;padding-left: 5px;padding-right: 5px;">

			<button id="xinzeng" class="btn btn-info btn-sm" onclick=addRow()><i class="fa fa-plus"></i>新增</button>
			<button id="xiugai" class="btn btn-info btn-sm" onclick=updateRow()><i
					class="glyphicon glyphicon-pencil"></i>修改</button>
			<button id="quxiao" class="btn btn-info btn-sm" onclick=cancelRow()><i
					class="glyphicon glyphicon-share-alt"></i>取消修改</button>
			<button id="shanchu" class="btn btn-info btn-sm" onclick=deleteRow()><i
					class="glyphicon glyphicon-remove"></i>删除</button>
			<button id="baocun" class="btn btn-info btn-sm" onclick=saveRow()><i
					class="glyphicon glyphicon-saved"></i>保存</button>
			<button id="daochu" class="btn btn-info btn-sm" onclick=download()><i
					class="glyphicon glyphicon-download"></i>导出Excel</button>
			<button id="daoru" class="btn btn-info btn-sm" onclick=$("#file").click();><i
					class="glyphicon glyphicon-upload"></i>导入Excel</button>
			<button id="xianshi" class="btn btn-info btn-sm" onclick=colsState(this)><i
					class="glyphicon glyphicon-align-justify"></i>显示隐藏列</button>

			<button id="reset" class="btn btn-info btn-sm" style="float: right;" onclick=reset()>重置</button>
			<button id="select" class="btn btn-info btn-sm" style="float: right;margin-right: 5px;"
				onclick=select()>查询</button>

			<div class="col-sm-3" style="float: right;width:170px;padding-left: 0px;">
				<input id="term" type="text" class="form-control" style="width:150px;height:30px;font-size: 12px;"
					id="txt_search_departmentname">
				<select class="form-control" style="width:150px;height:30px;font-size: 12px;display: none;"
					id="selectSearch"></select>
			</div>
			<label id="searchName" class="control-label col-sm-1"
				style="float: right;margin-top: 7px;padding: 0px;width: 60px;font-weight: normal;"
				for="txt_search_departmentname"></label>

			<input type="file" id="file" name="file" onchange="upload(this)" style="display:none" />
		</div>
	</div>
	<div id="tree"></div>
	<div id="table"></div>
	<div id="iframe" style="position: absolute;top: 0px;left: 200px;right: 0px;height: 820px;display:none">
		<iframe frameborder="0" width="100%" scrolling="No" height="100%" leftmargin="0" topmargin="0"
			id="sampleIframe"></iframe>
	</div>
	<div id="tabsEditData" class="ui-widget-content"
		style="position:absolute;top: 8%;left: 15%;width: 80%;height: 500px;background:#ffffff;overflow: hidden;border:1px solid #cccccc;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;display:none;z-index: 9999;">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 0px;">
			<legend id="tabTitle"></legend>
			<div onclick="closeTabsEditData();"
				style="position:absolute;top:10px;right:10px;width:17px;height:17px;background:url(../images/dialog_close.png) no-repeat;cursor:pointer;">
			</div>
		</fieldset>
		<div class="ui-widget-content"
			style="position:absolute;top: 35px;left: 0%;border: 0px;width: 100%;height: 93%;background:#ffffff;overflow: auto;">
			<iframe id="contentPanel" src="" frameborder="0" width="100%" scrolling="No" height="99%" leftmargin="0"
				topmargin="0"></iframe>
		</div>
	</div>
	<form action='page/pageInfo/201' method='post' id='form1' enctype="multipart/form-data">
		<input type="file" id="allDownload" name="allDownload" onchange="allDownloads(this)" style="display:none" />
		<input type="hidden" name="schemeid">
	</form>
	<form action='page/pageInfo/202' method='post' id='form2'>
		<input type="hidden" name="schemeid">
	</form>
</body>

</html>